package com.aidex.quartz.task;

import com.aidex.common.utils.JsonHelper;
import com.aidex.common.utils.http.HttpUtils;
import com.aidex.risk.domain.*;
import com.aidex.system.domain.Waybill;
import com.aidex.system.domain.WaybillDetail;
import com.aidex.system.service.WaybillDetailService;
import com.aidex.system.service.WaybillService;
import com.aidex.system.service.impl.GoodsRiskInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author JMY
 */
@Component("sendTask")
public class SendTask
{
    @Autowired
    private WaybillService waybillService;
    @Autowired
    private WaybillDetailService detailService;

    public tradeGoodsRiskApi sendTradeGoodsRiskApi() throws UnsupportedEncodingException {
        tradeGoodsRiskApi tradeGoodsRiskApi = new tradeGoodsRiskApi();
        trade_goods_risk_api trade_goods_risk_api = new trade_goods_risk_api();
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        trade_goods_risk_api.setSend_time(sdf.format(d));
        trade_goods_risk_api.setSender("航空货运安检模型");
        String str = UUID.randomUUID().toString();
        String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
        trade_goods_risk_api.setBusiness_no(temp);
        trade_goods_risk_api.setApi_desc("6.4综合风险等级、疑似风险标注");
        trade_goods_risk_api.setApi_type("");
        List<tradeGoodsRiskApiContent> contentList = new ArrayList<>();
        tradeGoodsRiskApiContent content = new tradeGoodsRiskApiContent();
        String json =JsonHelper.getInstance().writeValueAsString(createDemoTrade());
        Base64.Decoder decoder = Base64.getDecoder();
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] textByte = json.getBytes("UTF-8");
        final String encodedText = encoder.encodeToString(textByte);
        content.setContent(encodedText);
        contentList.add(content);
        trade_goods_risk_api.setRequest_list(contentList);

        tradeGoodsRiskApi.setTrade_goods_risk_api(trade_goods_risk_api);

        return tradeGoodsRiskApi;
    }

    public String sendTradeGoodsRiskApi1(String waybillNo) {
        Waybill waybill= waybillService.getUnSend(waybillNo);
        WaybillDetail detail = new WaybillDetail();
        detail.setWaybillId(waybill.getId());
        List<WaybillDetail> waybillDetails=detailService.findList(detail);

        String json =JsonHelper.getInstance().writeValueAsString(createTrade(waybill,waybillDetails));

        String res =HttpUtils.sendPost1("http://10.80.32.101:7080/ESBService/HRDEVAD",json);
        System.out.println("http://10.80.32.101:7080/ESBService/HRDEVAD" + ":(request)" + json+"  (response)"+res);
        return "http://10.80.32.101:7080/ESBService/HRDEVAD" + ":(request)" + json+"  (response)"+res;
    }
    static GoodsRiskInfo createTrade(Waybill waybill, List<WaybillDetail>  details)
    {
        GoodsRiskInfo info = new GoodsRiskInfo();
        tradeGoodsRiskHead head = new tradeGoodsRiskHead();
        head.setWayBillID(waybill.getWaybillNo()); //运单ID
        head.setWayBillNo(waybill.getWaybillNo()); //运单号
        head.setDataSource(waybill.getDataSource()=="1"?"CPFS":"ALMS"); //运单来源
        head.setRiskRating(waybill.getRiskRating()); //综合风险等级（低风险\优先\普通\高风险\严控）
        head.setRiskClassification(waybill.getRiskClassification()); //货物风险等级（普通\低风险\高风险）
        head.setAgentLevel(waybill.getAgentLevel()); //代理人等级
        head.setIsImportantFlights(waybill.getIsImportantFlights()); //重点航班标识
        info.setData(head);
        List<tradeGoodsRiskDetial> detials = new ArrayList<>();
        for (int i=0;i<details.stream().count();i++){
            tradeGoodsRiskDetial detial = new tradeGoodsRiskDetial();
            WaybillDetail item = details.get(i);
            detials.add(detial);
            detial.setTradeName(item.getTradeName()); //货品名
            detial.setTradeNameKey( item.getTradeNameKey()); //货品名关键词
            detial.setTradeType(item.getTradeType()); //货品名所属清单（0-无；1-X光机难辨识清单；2-疑似风险货物清单；3-隐含性危险品清单）
            detial.setTradeRiskClassification(item.getTradeRiskClassification()); //货物风险等级（普通\低风险\高风险）
            detial.setTradeRiskRating( item.getTradeRiskRating());//综合风险等级（低风险\优先\普通\高风险\严控）
        }
        head.setTradeGoodsRiskDetials(detials);
        return info;
    }


    static tradeGoodsRiskHead createDemoTrade()
    {
        tradeGoodsRiskHead head = new tradeGoodsRiskHead();
        head.setWayBillID("2021092712300005"); //运单ID
        head.setWayBillNo("123456"); //运单号
        head.setDataSource("ALMS"); //运单来源
        head.setRiskRating("高风险"); //综合风险等级（低风险\优先\普通\高风险\严控）
        head.setRiskClassification("高风险"); //货物风险等级（普通\低风险\高风险）
        head.setAgentLevel("D"); //代理人等级
        head.setIsImportantFlights("Y"); //重点航班标识

        List<tradeGoodsRiskDetial> detials = new ArrayList<>();
        tradeGoodsRiskDetial detial = new tradeGoodsRiskDetial();
        detials.add(detial);
        detial.setTradeName( "华为手机MATE10"); //货品名
        detial.setTradeNameKey( "华为手机"); //货品名关键词
        detial.setTradeType( "1"); //货品名所属清单（0-无；1-X光机难辨识清单；2-疑似风险货物清单；3-隐含性危险品清单）
        detial.setTradeRiskClassification( "高风险"); //货物风险等级（普通\低风险\高风险）
        detial.setTradeRiskRating( "高风险" );//综合风险等级（低风险\优先\普通\高风险\严控）

        detial = new tradeGoodsRiskDetial();
        detials.add(detial);
        detial.setTradeName( "华为手机MATE8"); //货品名
        detial.setTradeNameKey( "华为手机"); //货品名关键词
        detial.setTradeType( "1"); //货品名所属清单（0-无；1-X光机难辨识清单；2-疑似风险货物清单；3-隐含性危险品清单）
        detial.setTradeRiskClassification( "高风险"); //货物风险等级（普通\低风险\高风险）
        detial.setTradeRiskRating( "高风险" );//综合风险等级（低风险\优先\普通\高风险\严控）
        head.setTradeGoodsRiskDetials(detials);
        return head;
    }


/*
    static wlpt_agent_common_syncResponse queryTradeAgentCommonLevel(Waybill waybill) throws JsonProcessingException {
        wlpt_agent_common_sync response = new wlpt_agent_common_sync();
        response.agentCode=waybill.getAgentCode();
        response.agentName=waybill.getOrganization();
        response.sourceCode=waybill.getDataSource()=="CFPS"?"1":"4";
        String json =JsonHelper.getInstance().writeValueAsString(response);
        String res =HttpUtils.sendPost1("http://10.80.32.101:7080/ESBService/HRDEVAD",json);
        return JsonHelper.getInstance().readValue(res,wlpt_agent_common_syncResponse.class);
    }*/
    static wlpt_diff_producet_queryResponse queryDiffProducetCommonLevel(Waybill waybill) throws JsonProcessingException {
        wlpt_diff_producet_query  request = new wlpt_diff_producet_query();
        request.setDifferentiationProductCode(waybill.getDiffProductSn().split(","));
        String json =JsonHelper.getInstance().writeValueAsString(request);
        String res =HttpUtils.sendPost1("http://10.80.32.101:7080/ESBService/HRDEVAD",json);
        return JsonHelper.getInstance().readValue(res,wlpt_diff_producet_queryResponse.class);
    }
    static wlpt_agent_scoreResponse queryTradeAgentLevel(Waybill waybill) throws JsonProcessingException {
        wlpt_agent_score request = new wlpt_agent_score();
        request.setAgentCode(waybill.getAgentCode());
        request.setAgentName(waybill.getOrganization());
        request.setSourceCode(waybill.getDataSource()=="CFPS"?1:4);
        String json =JsonHelper.getInstance().writeValueAsString(request);
        String res =HttpUtils.sendPost1("http://10.80.32.101:7080/ESBService/HRDEVAD",json);
        return JsonHelper.getInstance().readValue(res,wlpt_agent_scoreResponse.class);
    }

}

